package com.application.adapter;


import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.application.R;
import com.application.bean.NewsBean;

import java.util.List;

public class MyRefreshAdapter extends BaseQuickAdapter<NewsBean, BaseViewHolder> {

    public MyRefreshAdapter(List<NewsBean> data) {
        super(R.layout.list_item_layout, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, NewsBean newsBean) {
        baseViewHolder.setText(R.id.tv_recycler_title,newsBean.getTitle());
        baseViewHolder.setText(R.id.tv_recycler_content,newsBean.getContent());
        baseViewHolder.setImageResource(R.id.iv_recycler_img,newsBean.getImageResourceId());
        View contentView = baseViewHolder.getView(R.id.rl_item_container);
        ViewGroup.LayoutParams params=contentView.getLayoutParams();
        if (getRecyclerView().getLayoutManager() instanceof StaggeredGridLayoutManager){
            int randomHeight= (int) (Math.random()*200+300);
            params.height=dp2px(randomHeight);
        }else {
            params.height=ViewGroup.LayoutParams.WRAP_CONTENT;
        }
        contentView.setLayoutParams(params);
    }
    private  int dp2px(float dpValue){
        getContext();
        final float scale=getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue*scale+0.5f);
    }
}
